using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections.Generic;

/// <summary>
/// Summary description for User
/// </summary>
public class User
{
    string username;
    char[] zip;
    int range;
    Location loc;
    PoolLocation poolLocation;

    public PoolLocation LocationInPool 
    { get { return poolLocation; } set { poolLocation = value; } }

    public struct PoolLocation
    {
        public SortedList<int, AreaGrid> longi;
        public AreaGrid lati;
        public PoolLocation(SortedList<int, AreaGrid> longitude, AreaGrid latitude)
        { longi = longitude; lati = latitude; }
    }

    DateTime lastCommunicated;
    DateTime lastReceiveTime;
    
    public User(string UserName, string ZipCode, int range)
    {
        this.username = UserName;
        this.range = range;
        if (this.range > 50) this.range = 50; //protection
        this.loc = Geographics.GetLocation(ZipCode);
        this.lastCommunicated = DateTime.Now;
        this.LastReceiveTime = DateTime.MinValue;

        if (double.IsInfinity(loc.Longitude)) //search failed
            throw new ZipNotFoundException();

        this.zip = ZipCode.ToCharArray(0, 5);
        for (int i = 0; i < 5; i++)
            if (!Char.IsDigit(zip[i]))
                throw new ZipNotValidException();
    }

    public Location Location
    {
        get { return loc; }
    }

    public DateTime LastCommunicated { 
        get { return lastCommunicated; }
        set { lastCommunicated = value; }
    }
    public DateTime LastReceiveTime
    {
        get { return lastReceiveTime; }
        set { lastReceiveTime = value; }
    }
    public int Range
    {
        get { return range; }
        set { range = value; }
    }

    public string UserName
    {
        get { return username; }
    }

    public string ZipCode
    {
        get
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < zip.Length; i++) sb.Append(zip[i]);
            return sb.ToString(); }
    }

    public class ZipNotValidException : Exception { }
    public class ZipNotFoundException : Exception { }


}
